package com.example.zsf.activity.mine;

import android.graphics.PointF;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.dlazaro66.qrcodereaderview.QRCodeReaderView;
import com.example.zsf.R;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ScanActivity extends AppCompatActivity implements QRCodeReaderView.OnQRCodeReadListener {

    @BindView(R.id.QRCodeReaderView)
    com.dlazaro66.qrcodereaderview.QRCodeReaderView QRCodeReaderView;
    @BindView(R.id.v_im)
    ImageView vIm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        ButterKnife.bind(this);
        initQRView();

        Animation verticalAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0.9f);
        verticalAnimation.setDuration(1500); // 动画持续时间
        verticalAnimation.setRepeatCount(Animation.INFINITE);
        verticalAnimation.setInterpolator(new LinearInterpolator());
        vIm.setAnimation(verticalAnimation);
    }


    private void initQRView() {
        QRCodeReaderView.setAutofocusInterval(2000L);
        QRCodeReaderView.setQRDecodingEnabled(true);
        QRCodeReaderView.setOnQRCodeReadListener(ScanActivity.this);
        QRCodeReaderView.setBackCamera();
    }

    @Override
    protected void onResume() {
        super.onResume();
        QRCodeReaderView.startCamera();
    }

    @Override
    protected void onStop() {
        super.onStop();
        QRCodeReaderView.stopCamera();
    }

    @Override
    public void onQRCodeRead(String text, PointF[] points) {
        Logger.e(text);
        finish();
    }

    @OnClick(R.id.cancel_im)
    public void onViewClicked() {
        finish();
    }
}